#pragma once

#include <AVUIObjectCollection.h>
#include <AVUIVisual3D.h>

namespace AVUI {

class Visual3D;
class IVisual3DContainer;

class Visual3DCollection : public TObjectCollection<Visual3D>
{
public:
    DECLARE_ELEMENT(Visual3DCollection, ObjectCollectionBase);

    Visual3DCollection(IVisual3DContainer* pOwner = NULL) : m_pOwner(pOwner)
    {
        
    }

protected:

	virtual void ClearItems()
	{
		for(int idx = 0; idx < get_Count(); idx++)
		{
			OnRemove((*this)[idx]);
		}

		TObjectCollection<Visual3D>::ClearItems();
	}

	virtual void InsertItem(int index, Visual3D* pItem)
	{
		TObjectCollection<Visual3D>::InsertItem(index, pItem);

		OnAdd(pItem);
	}

	virtual void RemoveItem(int index)
	{
		TRefCountedPtr<Visual3D> pOldValue = (*this)[index];
		TObjectCollection<Visual3D>::RemoveItem(index);

		OnRemove(pOldValue);
	}

	virtual void SetItem(int index, Visual3D* pItem)
	{
		TRefCountedPtr<Visual3D> pOldValue = (*this)[index];
		TObjectCollection<Visual3D>::SetItem(index, pItem);

		OnRemove(pOldValue);
		OnAdd(pItem);
	}

private:

	void OnAdd(Visual3D* pAdd)
	{
        m_pOwner->AddChild(pAdd);
	}

	void OnRemove(Visual3D* pRemove)
	{
        m_pOwner->RemoveChild(pRemove);
	}

    IVisual3DContainer* m_pOwner;
};

}; // namespace AVUI
